﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyWrite.API.Appicant.Command;
using MediatR;

namespace B.S.DataBase.MyWrite.API.Appicant.CommandHandler
{
    public class DelMenuCommendHandlers : IRequestHandler<DelMenuCommends, APIResult<Menu>>
    {
        private readonly IBaseRepository<Menu> menuRepository;
        private readonly IMapper mapper;

        public DelMenuCommendHandlers(IBaseRepository<Menu> menuRepository, IMapper mapper)
        {
            this.menuRepository = menuRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<APIResult<Menu>> Handle(DelMenuCommends request, CancellationToken cancellationToken)
        {
            APIResult<Menu> result = new APIResult<Menu>();
            var menus=mapper.Map<Menu>(request);
            var delMenu = menuRepository.GetAll().Where(p => p.MenuID == request.MenuID || p.ParentMenuId == request.MenuID ).ToList();
            var data=menuRepository.DelList(delMenu).Result;
            if (data>0)
            {
                result.Code = APIEnum.删除成功;
                result.Msg=APIEnum.删除成功.ToString();
                result.data = menus;

            }
            return Task.FromResult(result);
        }
    }
}
